<?php

/**
 * Albireo Kernel
 *
 * @copyright  Copyright (c) 2010 Albireo Solutions
 * @package    Kernel
 */
namespace KernelModule\LogsModule\PhpModule;

use Nette\Environment;
use Nette\Debug;
use Nette\Forms\Form;

use \KernelModule\BasePresenter;
use Albireo\DatePicker;

use Albireo\Forms\AlbireoForm;

use KernelModule\LogsModule\PageOutOfBoundsException;
use KernelModule\LogsModule\LogsException;

/**
 * Displays the PHP error logs list.
 *
 * @author     Pavel
 * @package    Kernel
 */
class DisplayPresenter extends BasePresenter {

    public function actionCheck() {
        $logModel = new Process();
        try {
            if ($logModel->workOrDirtyExist()) {
                $this->redirect('Process:');
            } else {
                //TODO $this->view('default'); should be better here, just
                //paste it and test it
                $this->redirect('Display:');
            }
        } catch (LogsException $e) {
            $this->redirect(':Kernel:Logs:Error:fsProblem');
        }
    }

    public function renderDefault($position = 1, $back = false) {
        $this->showMenu();
        $dispModel = new Display();
        $count = $dispModel->getLimit();

        try {
            $this->template->logArray = $dispModel->handleLogs($count, $position, $back);
        } catch (PageOutOfBoundsException $e) {
            $this->redirect('Display:');
        } catch (LogsException $e) {
            $this->redirect(':Kernel:Logs:Error:');
        }

        $curPosition = $dispModel->getCleanPointer();
        
        $this->template->title = _("PHP Error Logs");
        $this->template->curPosition = ($back ? $curPosition : $position);
        $this->template->limit = $count;
        $this->template->eofNext = ($dispModel->isEofInTheNextLoad() ? true : false);
        $this->template->position = ($back ? $position : $curPosition);
        $this->template->positionPage = round(($back ? $curPosition : $position) / ($count * 200)) + 1;
    }

    protected function createComponentDatepickerForm() {
        $datePicker = new DatePicker();
        return $datePicker->getDatePicker($this);
    }

    public function datePickerSubmitted($form) {
        $values = $form->values;
        $searcher = new Search();
        try {
            $position = $searcher->findPosition($values['date']);
        } catch (LogsException $e) {
            $this->redirect(':Kernel:Logs:Error:');
        }
        $this->redirect("default", array("position" => $position));
        //$page = ($line / $searcher->getLimit()) + 1;
    }

}
